package com.minivision.sdktestdemo.utils;

import android.content.Context;
import android.util.SparseArray;
import android.view.View;

import com.minivision.sdktestdemo.controler.ui.list.ListControler;
import com.minivision.sdktestdemo.model.UriInfoGetter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by devtang on 2016/4/11.
 */
public class SimpleListUtils {
    public static String TV_TITLE = "title";
    public static String TV_CONTENT = "content";
    public static String IMG_ICON = "pic";

    public static List<? extends Map<String, ?>> getMusicList(ListControler listControler, String []uris) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Set<String> set = new HashSet<String>(Arrays.asList(uris));
        StringBuilder builder = new StringBuilder();
        UriInfoGetter getter = (UriInfoGetter) listControler.getmProvider();
        for (int i = 0; i < getter.uriSize(); ++i) {
            if (set.contains(getter.getUri(i)) && set.size() != 0) {
                builder.setLength(0);
                Map<String, Object> map = new HashMap<String, Object>();
                //添加标题显示
                builder.append(listControler.getTitle(i));
                map.put(TV_TITLE, builder.toString());

                builder.setLength(0);
                //添加内容显示
                builder.append(listControler.getContent(i));
                map.put(TV_CONTENT, builder.toString());

                list.add(map);
            }
        }
        return list;
    }

    public static List<? extends Map<String, ?>> getMusicList(ListControler listControler) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < listControler.size(); ++i) {
            builder.setLength(0);
            Map<String, Object> map = new HashMap<String, Object>();
            //添加标题显示
            builder.append(listControler.getTitle(i));
            map.put(TV_TITLE, builder.toString());

            builder.setLength(0);
            //添加内容显示
            builder.append(listControler.getContent(i));
            map.put(TV_CONTENT, builder.toString());

            list.add(map);
        }
        return list;
    }

    public static List<? extends Map<String, ?>> getClassfiedList(ListControler listControler, Context context) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < listControler.size(); ++i) {
            builder.setLength(0);
            Map<String, Object> map = new HashMap<String, Object>();
            if (listControler.getPicUrl(i) == null) {
                int id = context.getResources().getIdentifier("ic_action_user","drawable",context.getPackageName());
                map.put(IMG_ICON, id);
            }

            //添加标题显示
            builder.append(listControler.getTitle(i));
            map.put(TV_TITLE, builder.toString());

            builder.setLength(0);
            //添加内容显示
            builder.append(listControler.getContent(i));
            map.put(TV_CONTENT, builder.toString());

            list.add(map);
        }
        return list;
    }

    public static <T extends View> T getView(View view, int id) {
        SparseArray<View> sparseArray = (SparseArray<View>) view.getTag();
        if (sparseArray == null) {
            sparseArray = new SparseArray<View>();
            view.setTag(sparseArray);
        }
        View childView = sparseArray.get(id);
        if (childView == null) {
            childView = view.findViewById(id);
            sparseArray.put(id, childView);
        }
        return (T) childView;
    }
}
